package com.icesoft.system.service.impl;

import com.icesoft.framework.core.service.impl.BaseServiceImpl;
import com.icesoft.framework.core.util.IpUtil;
import com.icesoft.system.entity.LoginRecord;
import com.icesoft.system.handler.LoginHandler;
import com.icesoft.system.mapper.LoginRecordMapper;
import com.icesoft.system.vo.UserVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Service
@RequiredArgsConstructor
public class LoginRecordServiceImpl extends BaseServiceImpl<LoginRecordMapper, LoginRecord> implements LoginHandler {

	private final AccountServiceImpl accountService;

	private final UserServiceImpl userService;

	@Override
	public void success(HttpServletRequest request, String loginType, UserVO user) {
		String ipAddr = IpUtil.getIpAddr(request);
		LoginRecord loginRecord = new LoginRecord();
		loginRecord.setUserId(user.getId());
		loginRecord.setMessage("登录成功");
		loginRecord.setError(false);
		loginRecord.setIp(ipAddr);
		loginRecord.setCity(IpUtil.getCityInfo(request));
		createEntity(loginRecord);
	}


	@Override
	public void error(HttpServletRequest request, String loginType, UserVO user, Exception e) {
		String ipAddr = IpUtil.getIpAddr(request);
		LoginRecord loginRecord = new LoginRecord();
		loginRecord.setUserId(user.getId());
		loginRecord.setMessage(e.getMessage());
		loginRecord.setError(true);
		loginRecord.setIp(ipAddr);
		loginRecord.setCity(IpUtil.getCityInfo(request));
		createEntity(loginRecord);
	}
}
